<?php
// 权限控制器
class RoleAction extends CommonAction {
    /**
     * [role description]权限模版跳转
     * @return [type] [description]
     */
	public function role(){
		$this->display("Role:role");
	}

    /**
     * [getRoleList description]获取权限数据
     * @return [type] [description]
     */
	public function getRoleList(){
		$role=M('Role');
		$roleData=$role->select();
		return $roleData;
	}

	/**
     * 获取角色数据
     */
    public function getRoleData()
    {
        $result = $this->getRoleList();
        $list = array();
        foreach ($result as $value)
        {
            $m = new stdClass();
            $m->id= intval($value['role_id']);
            $m->text = $value['role_name'];
            $m->iconCls = 'icon-notice';
            $list[] = $m;
        }
        echo json_encode($list);
    }

    /**
     * 添加角色
     * @param type $name
     */
    public function addRole()
    {
    	$role=M('Role');
    	$role->role_name=I('name');
    	$role->add();
    }
    
	/**
     * 获取角色名称
     */
    public function getRoleByUsreID($id)
    {
        $model = $this->db->select('*')->from('role')
            ->join('user','user.role_id=role.role_id','left')
            ->where('id',$id)
            ->get();
            
        return $model->first_row()->role_name;
    }
	
    /**
     * 删除角色
     * @param type $id
     */
    public function removeRole()
    {
    	$role_id=I('id');
    	$role=M('Role');
    	$role->where("role_id='".$role_id."'")->delete();
    }
    
    /**
     * 编辑角色
     */
    public function editRole()
    {
    	$role_id=I('id');
    	$role=M('Role');
    	$data['role_name']=I('name');
    	$role->where("role_id='".$role_id."'")->save($data);
    }


    /**
     * 获取权限列表
     */
    public function getPermissionData()
    {
        $cresult = $this->getRolePermission($_GET['roleId']);
        
        $permission = M('Permission');
        $oldList =  $permission->select();
        
        $str = '[';
        
        foreach ($oldList as $value)
        {
            if ($value['pmn_ischild'] == 0)
            {
                $str .=sprintf('{"        id":"%s","text": "%s","iconCls":"icon-notice",%s"children":[', $value['pmn_id'], $value['pmn_name'],
                    $this->isInPermissionList($cresult, $value['pmn_id']));
                $subList = $permission->where("pmn_pcode='".$value['pmn_code']."'")->select();
                foreach ($subList as $subValue)
                {
                    $str .=sprintf('{"id":"%s","text": "%s",%s"iconCls":"icon-notice"},', $subValue['pmn_id'], $subValue['pmn_name'],
                        $this->isInPermissionList($cresult, $subValue['pmn_id']));
                }
                $str = substr($str,0,-1);
                $str .=']},';
                
            }
        }
        $str = substr($str,0,-1);
        $str .="]";
        echo $str;
    }

    /**
     * 获取角色的权限列表
     * @param $roleId 角色编号
     */
    private function getRolePermission($roleId)
    {
        $cresult = array();
        if ($roleId)
        {
        	$role_permission = M('Role_permission');
            $cresult = $role_permission->where("role_id='".$roleId."'")->find();
            if ($cresult)
            {
                $cresult = $role_permission->where("role_id='".$roleId."'")->select();
            }
            else
            {
                $cresult = array();
            }
        }
        return $cresult;
    }
    
    /**
     * 判断是否在权限列表之内
     * @param $pmn_id 权限编号
     * @param $pmn_id 权限编号
     */
    private function isInPermissionList($list, $pmn_id)
    {
        foreach ($list as $value)
        {
            if ($value['pmn_id'] == $pmn_id)
            {
                return '"checked" : true,';
            }

        }
        
        return '';
    }

    /**
     * 添加权限
     */
    public function addPermission()
    {
    	$pmnId = I('pmnId');
    	$roleId = I('roleId');
        
        //删除现在权限
        $this->deletePermission($roleId);
        
        $role_permission = M('Role_permission');
        foreach ($pmnId as $value) 
        {
        	$role_permission->role_id=$roleId;
        	$role_permission->pmn_id=$value;
        	$role_permission->add();
        }
    }
    
    /**
     * 删除权限
     */
    public function deletePermission($roleId)
    {
    	$role_permission = M('Role_permission');
    	$role_permission->where("role_id='".$roleId."'")->delete();
    }

    /**
     * [havePermission description]验证权限
     * @return [type] [description]
     */
    public function havePermission()
    {
        $employe=M('Employe');
        $employe_id=session('employe_id');
        $employe_role=$employe->where("employe_id='".$employe_id."'")->getField("employe_role");

        $model=new Model();
        $list=$model->table('oa_role_permission orp,oa_permission op')->where("orp.pmn_id=op.pmn_id and orp.role_id='".$employe_role."'")->select();

        $nlist=array();
        foreach ($list as $value) {
            $nlist[]=$value['pmn_name'];
        }
        return $nlist;

        /**登录用户编号
        $userId = $this->session->userdata('USERID');
        
        //获取该用户的角色
        $roleId = $this->db->select('role_id')->from('user')->where('id',$userId)
            ->get()->first_row();
        $roleId = $roleId->role_id;
        
        //获取用户的权限列表
        $list = $this->db->select('pmn_name')->from('role_permission')
            ->join('permission','role_permission.pmn_id=permission.pmn_id', 'left')
            ->where('role_id',$roleId)
            ->get()->result();
       
       $nlist = array();
       foreach ($list as $value)
       {
            $nlist[] = $value->pmn_name;
       }
       return $nlist;
       */
    }
}